package com.ycg.biquge.common;

import lombok.Data;
import lombok.Getter;

import java.io.Serializable;

/**
 * @author YCG
 * @date 2024-04-29 21:11
 * @description 统一结果封装
 */
@Data
public class Result<T> implements Serializable {

    private  int code;
    private  String msg;
    private  T data;

    public static <T> Result<T> ok(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.Success.getCode());
        result.setMsg(ResultCode.Success.getDesc());
        result.setData(data);
        return result;
    }

    public static <T> Result<T> error(String errMsg) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.Error.getCode());
        result.setMsg(errMsg);
        return result;
    }


    enum ResultCode {
        Success(0,"响应成功"),
        Error(1,"响应失败");

        private final int code;
        private final String desc;

        ResultCode(int code,String desc) {
            this.code = code;
            this.desc = desc;
        }

        public int getCode() {
            return code;
        }

        public String getDesc() {
            return desc;
        }

    }
}
